<?php
/**
 *
 *
 * @author hzz
 * @date 2018-9-5
 * @copyright PanxSoft Inc.
 */

namespace App\Http\Controllers\WebPage\Seo;


use App\Http\Controllers\Controller;

use App\Http\Models\Article;
use App\Http\Models\ArticleTag;
use App\Http\Requests\Home\NewsIndexRequest;
use App\Services\Utils\ApiChecker;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\View\View;
use Jaybizzle\CrawlerDetect\CrawlerDetect;

class NewsController extends Controller
{
    use ApiChecker;

    const ROUTES = [
        'index' => 'seo-news.index',
    ];

    const PROJECT_CASE_ROUTES = [
        'index' => 'seo-project-case.index',
    ];

    const SOLUTION_CASE_ROUTES = [
        'index' => 'seo-solution-case.index',
    ];

    /**
     * @param NewsIndexRequest $request
     * @return View
     *
     * @throws \Exception
     */
    public function index(NewsIndexRequest $request)
    {
        $url = $request->url();
        $view = $this->routeArticleTagMap()[$url]['view'];

        if (! (new CrawlerDetect)->isCrawler()) return view($view);

        if ($request->detail_id) {
            /** @var Article $article */
            $article = (new Article)->find($request->detail_id);
                $this->checkItemExist($article, '新闻');

            $title = $article->title;
            $keywords = $article->keywords;
            $description = $article->description;
        } else {
            $sub_type_ids = $this->getSubCategory()->pluck('id')->all();
            $article_type_id = $request->cid ?: $request->article_type_id;

            $articles = (new Article)
                ->filterArticleTypeId($article_type_id)
                ->whereIn('article_type_id', $sub_type_ids)
                ->latest('sort')
                ->paginate($request->per_page ?: 8);
        }

        return view($view)->with(compact(
            'articles', 'article', 'title', 'keywords', 'description'
        ));
    }

    /**
     * @return Collection|ArticleTag[]
     *
     * @throws \Exception
     */
    private function getSubCategory()
    {
        $url = request()->url();

        $article_tag_id = $this->routeArticleTagMap()[$url]['id'];
        $article_tag_name = $this->routeArticleTagMap()[$url]['name'];

        $articleTag = (new ArticleTag)->find($article_tag_id);
            $this->checkItemExist($articleTag, $article_tag_name);

        // 返回解决方案的二级菜单
        $sub_types = $articleTag->tags()
            ->latest('sort')
            ->get();

        return $sub_types;
    }

    /**
     * @return array
     */
    private function routeArticleTagMap()
    {
        $map = [
            route(self::ROUTES['index'])               => [
                'id'   => ArticleTag::COMPANY_NEWS,
                'name' => '企业动态',
                'view' => 'official_website.company_news',
            ],
            route(self::SOLUTION_CASE_ROUTES['index']) => [
                'id'   => ArticleTag::SOLUTION_CASE,
                'name' => '解决方案',
                'view' => 'official_website.solution_case',
            ],
            route(self::PROJECT_CASE_ROUTES['index'])  => [
                'id'   => ArticleTag::PROJECT_CASE,
                'name' => '工程案例',
                'view' => 'official_website.project_case',
            ],
        ];

        return $map;

    }
}